<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8">
    <title>订单列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" href="/admin/layui/css/layui.css" media="all"/>
</head>
<body>

<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

<script type="text/html" id="toolbarDemo">

</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="refund">退款</a>
</script>

<!--<script type="text/html" id="titleTpl">-->
    <!--<a href="/detail/{{d.id}}" class="layui-table-link">{{d.title}}</a>-->
<!--</script>-->

<script src="/admin/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['table','jquery','element','form','layer'], function(){
        var table = layui.table
            , $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , element = layui.element;

        table.render({
            elem: '#tableId'
            ,url:'/order/getAllTrades'
            ,toolbar: '#toolbarDemo'
            ,cellMinWidth: 200 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,title: '用户数据表'
            ,limit:20
            ,totalRow:true
            ,cols: [        //  "[["需要将两个括号拆开换行，因为[[…]]之间的表达式在Thymeleaf被认为是内联表达式
                [
                    // {type: 'checkbox', fixed: 'left'},
                    {field:'tid', title:'订单号', width:200, unresize: false, align:'center',sort: false}
                    ,{field:'orderStatusStr', title:'订单状态', width:200, align:'center'}
                    ,{field:'payment', title:'订单总额(元)', width:150, align:'center',sort: false,templet: function(res){
                            return '<em>'+ res.payInfo.payment +'</em>'
                        }}
                    // ,{field:'payTypeStr', title:'支付类型', width:120, edit: 'text', sort: true,align:'center'}
                    ,{field:'fansNickname', title:'购买人名称', width:230, sort: false,align:'center',templet:function(res){
                        return '<span>'+ res.buyer.fansNickname +'</span>'
                    }}
                    ,{field:'tradeSource', title:'平台', width:200,align:'center',templet:function (res) {
                        return '<span>'+ res.tradeSource.platform +'</span>'
                    }}
                    ,{field:'createTime', title:'下单时间', width:200,align:'center'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150,align:'center'}
                ]]
            ,page: true,id:'tradesSold'
        });

        //监听搜索
        form.on('submit(member-search)', function(data){
            var field = data.field;
            var phone = JSON.stringify(data.field).phone;
            if(!phone){
                return;
            }
            //执行重载
            table.reload('tableId', {
                where: field
            });
        });

        $(document).on("click", "#searchMember", function () {
            var phone = $("#phone").val();
            table.reload('salesManTable', {
                where: {phone: phone}
            });
        })


        //头工具栏事件
        table.on('toolbar(tableFilter)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(tableFilter)', function(obj){
            var data = obj.data;
            console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'refund'){

                if(data.orderStatus == 'TRADE_CLOSED'){
                    layer.alert("订单成功才允许退款操作！",{icon:0});
                    return;
                }
                // alert("下来了");
                $.ajax({
                    type: "POST",
                    url: '/order/refundSell',
                    async:false,//同步提交。不设置则默认异步，异步的话，最后执行ajax
                    data:{
                        tid: data.tid,
                        refundFee:data.payInfo.payment,
                        oid:  data.orderItems[0].oid,
                        desc:'商家自动退款',
                    },
                    success: function(result) {
                        if(result.status == 200){
                            layer.alert("退款成功！",{icon:1});
                        }else{
                            layer.alert(result.message,{icon:0});
                        }
                    },
                    error: function(error) {
                        alert(error.status);
                    }
                });

                // layer.prompt({
                //     formType: 2
                //     ,value: data.email
                // }, function(value, index){
                //     obj.update({
                //         email: value
                //     });
                //     layer.close(index);
                // });
            }
        });
    });
</script>

</body>
</html>